import type { StyleProp, ViewStyle } from 'react-native';

export interface DeviceInfo {
  id: string;
  name: string;
  model?: string;
  manufacturer?: string;
  ipAddress?: string;
  type: 'dlna' | 'miracast';
  controlURL?: string;
  renderControlURL?: string;
}

export interface PlaybackStatus {
  isPlaying: boolean;
  duration: number;
  position: number;
  volume: number;
  rate?: number;
  isMuted?: boolean;
  bufferedPosition?: number;
  bufferedDuration?: number;
  isBuffering?: boolean;
  isReadyToPlay?: boolean;
  isCompleted?: boolean;
  error?: string;
}

// 我们保留一些基本类型，但移除iOS特有内容

export type ExpoDlnaPlayerModuleEvents = {
  onDeviceFound: (device: DeviceInfo) => void;
  onDeviceDisappeared: (deviceId: string) => void;
  onConnectionChanged: (params: ConnectionChangedEventPayload) => void;
  onPlaybackStatusChanged: (status: PlaybackStatus) => void;
  onError: (params: ErrorEventPayload) => void;
  onPermissionStatus: (params: PermissionStatusEventPayload) => void;
};

export type ErrorEventPayload = {
  code: string;
  message: string;
  deviceId?: string;
};

export type PermissionStatusEventPayload = {
  permission: 'localNetwork' | 'bluetooth' | 'location';
  status: 'granted' | 'denied' | 'unavailable';
};

export type ConnectionChangedEventPayload = {
  deviceId: string;
  connected: boolean;
}

export type ExpoDlnaPlayerViewProps = {
  autoDiscover?: boolean;
  style?: StyleProp<ViewStyle>;
};

export interface ProjectionMode {
  SCREEN_MIRRORING: string;
  VIDEO_ONLY: string;
}

interface EventEmitterInterface<T> {
  addListener(eventType: keyof T, listener: T[keyof T]): { remove: () => void };
  removeListener(eventType: keyof T, listener: T[keyof T]): void;
  removeAllListeners(eventType?: keyof T): void;
  emit(eventType: keyof T, ...args: any[]): void;
  listenerCount(eventType: keyof T): number;
}

export interface DLNAPlayerInterface extends EventEmitterInterface<ExpoDlnaPlayerModuleEvents> {
  // 设备搜索相关
  startDiscovery(): Promise<void>;
  stopDiscovery(): Promise<void>;
  getDevices(): Promise<DeviceInfo[]>;
  
  // 连接与控制
  connectToDevice(deviceId: string): Promise<boolean>;
  disconnectFromDevice(): Promise<void>;
  isConnected(): Promise<boolean>;
  getConnectedDevice(): Promise<DeviceInfo | null>;
  
  // 媒体控制 - 核心方法
  play(url: string, title?: string, mimeType?: string): Promise<void>;
  pause(): Promise<void>;
  resume(): Promise<void>;
  stop(): Promise<void>;
  seek(position: number): Promise<void>;
  setVolume(volume: number): Promise<void>;
  getPlaybackStatus(): Promise<PlaybackStatus>;
  
  // 扩展媒体控制
  setRate?(rate: number): Promise<void>;
  setMuted?(muted: boolean): Promise<void>;
  getBufferingStatus?(): Promise<object>;
  
  // Miracast特定功能 - 仅Android支持
  startProjection(deviceId: string, mode?: string): Promise<boolean>;
  stopProjection(): Promise<void>;
  isProjectionSupported(): Promise<boolean>;
}

export type ConnectionStatus = {
  deviceId: string;
  connected: boolean;
};
